package com.wordsay.context;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lionsoul.jcseg.core.ADictionary;
import org.lionsoul.jcseg.core.DictionaryFactory;
import org.lionsoul.jcseg.core.JcsegTaskConfig;

import com.wordsay.entity.People;
import com.wordsay.util.PropertiesUtil;
import com.wordsay.util.TalkUtil;

public class Context {

    private static Logger log = LogManager.getLogger(Context.class.getName());  

	private static String WORDSAY_PROPERTIES = "config/wordsay.properties";
	
	private static Context instance;
	
	private Resource resource;
	
	private People self;
	
	private Map<String,String> contextMap;
	
	private JcsegTaskConfig jcseConfig; //分词配置
	
	private ADictionary dic; //分词词库
	
	private Map<String,String> stopWordsMap;
	
	private Map<String,String> similarPersonRange;
	
	private Context(){
		
		resource = new Resource();
		self = new People();
		contextMap = new HashMap<String,String>();
        stopWordsMap = new HashMap<String,String>();
        similarPersonRange = new HashMap<String,String>();
	}
	
	
	
	public static Context getInstance(){
		
		if(instance == null){
			
			try {
				init();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return instance;
	}
	
	public static void setWordsayProperites(String path){
		
		WORDSAY_PROPERTIES = path;
	}
	
	private static void init() throws Exception{
		
		log.info("start init the wordsay context ...");
		instance = new Context();
		
		log.info("init the wordsay.properties");
	    Properties properties = PropertiesUtil.getProperties(WORDSAY_PROPERTIES);	
	    if(properties == null) {
	    	
	    	throw new Exception("未能找到wordsay.properties配置文件["+WORDSAY_PROPERTIES+"]");
	    	
	    }
	    	    
	    String selfName = new String(properties.getProperty("selfName"));
	    instance.self.setName(selfName);
	    instance.self.setId(properties.getProperty("selfId"));
	    instance.self.setNickName(properties.getProperty("selfNickname"));
	    
	    instance.contextMap.put("wordsPath", properties.getProperty("wordsPath"));
	    instance.contextMap.put("wordsType", properties.getProperty("wordsType"));
	    instance.contextMap.put("isFilterStopuseWords", properties.getProperty("isFilterStopuseWords"));
	    instance.contextMap.put("similarWordsBaseCount", properties.getProperty("similarWordsBaseCount"));
	    
	    String[] similarRangePersons = properties.getProperty("similarPersonRange").split(",");
	    for(int i = 0; i < similarRangePersons.length;i++){
	    	instance.similarPersonRange.put(similarRangePersons[i],similarRangePersons[i] );
	    }
	    
	    
	    String stopWordsFile =  properties.getProperty("stopusewords");
	    instance.contextMap.put("stopWordsPath",stopWordsFile);

	    String jcsePropPath = properties.getProperty("jcseProperties");
	    instance.contextMap.put("jcsePropPath",jcsePropPath);	    

	    log.info("init the Jcseg config");
	    instance.jcseConfig = new JcsegTaskConfig(jcsePropPath);
	    
	    log.info("init the jcseg adictionary");
	    instance.dic = DictionaryFactory.createDefaultDictionary(instance.jcseConfig,instance.jcseConfig.isAutoload()); 
	    
	    log.info("init the stop words");
	    instance.stopWordsMap = TalkUtil.readStopWords(stopWordsFile);
	    
	    log.info("wordsay context init completed");
	}
	
	public Resource getResource() {
		return resource;
	}

	public People getSelf() {
		return self;
	}

	public Map<String, String> getContextMap() {
		return contextMap;
	}

	public JcsegTaskConfig getJcseConfig() {
		return jcseConfig;
	}

	public ADictionary getDic() {
		return dic;
	}

	public Map<String, String> getStopWordsMap() {
		return stopWordsMap;
	}

	public void setStopWordsMap(Map<String, String> stopWordsMap) {
		this.stopWordsMap = stopWordsMap;
	}

	public Map<String, String> getSimilarPersonRange() {
		return similarPersonRange;
	}

	public void setSimilarPersonRange(Map<String, String> similarPersonRange) {
		this.similarPersonRange = similarPersonRange;
	}
	
	
	
  }